package com.coastline.fruit.common.utils;

import com.coastline.fruit.common.exception.CoastLineFruitException;
import com.coastline.fruit.common.exception.CoastlineFruitErrorInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;

/**
 * 参数校验工具类
 *
 * @author Coastline
 * @date 2025/1/18 16:12
 */
@Slf4j
public final class ValidateUtils {
    /**
     * 参数必填性校验
     *
     * @param para 待校验参数
     * @param filedName 参数名称
     * @param coastlineFruitErrorInfo 异常信息
     * @param <T> 泛型
     * @throws CoastLineFruitException 异常
     */
    public static <T> void checkMandatoryFiled(T para, String filedName, CoastlineFruitErrorInfo coastlineFruitErrorInfo) throws CoastLineFruitException {
        if (para instanceof String) {
            if (StringUtils.isBlank((String)para)) {
                log.error("{} is empty", filedName);
                throw new CoastLineFruitException(coastlineFruitErrorInfo);
            }
        } else if (para instanceof Collection<?>) {
            if (CollectionUtils.isEmpty((Collection<?>) para)) {
                log.error("{} is empty", filedName);
                throw new CoastLineFruitException(coastlineFruitErrorInfo);
            }
        } else {
            if (null == para) {
                log.error("{} is empty", filedName);
                throw new CoastLineFruitException(coastlineFruitErrorInfo);
            }
        }
    }

    /**
     * 校验参数的长度
     *
     * @param para 待校验参数
     * @param filedName 参数名称
     * @param maxLength 最大长度
     * @param coastlineFruitErrorInfo 异常信息
     * @param <T> 泛型
     * @throws CoastLineFruitException 异常
     */
    public static <T> void checkLength(T para, String filedName, int maxLength, CoastlineFruitErrorInfo coastlineFruitErrorInfo) throws CoastLineFruitException {
        if (para instanceof String) {
            if (((String) para).getBytes(StandardCharsets.UTF_8).length > maxLength) {
                log.error("{} length exceed max length", filedName);
                throw new CoastLineFruitException(coastlineFruitErrorInfo);
            }

        } else if (para instanceof Collection<?>) {
            if (((Collection<T>) para).size() > maxLength) {
                log.error("{} length exceed max length", filedName);
                throw new CoastLineFruitException(coastlineFruitErrorInfo);
            }
        }
    }

    /**
     * 校验参数值的合法性
     *
     * @param para 待校验参数
     * @param typeList 枚举值集合
     * @param filedName 参数名称
     * @param coastlineFruitErrorInfo 异常信息
     * @param <T> 泛型
     * @throws CoastLineFruitException 异常
     */
    public static <T> void checkTypeList(T para, List<T> typeList, String filedName, CoastlineFruitErrorInfo coastlineFruitErrorInfo) throws CoastLineFruitException {
        if (!typeList.contains(para)) {
            log.error("{} not in the enumeration value", filedName);
            throw new CoastLineFruitException(coastlineFruitErrorInfo);
        }
    }
}
